<?php


namespace App\Common\Validate;
use Hyperf\Di\Annotation\Inject;
use App\Exception\ValidateException;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
use Hyperf\Validation\ValidatorFactory;

class Validate extends ValidatorFactory
{
    /**
     * @Inject()
     * @var ValidatorFactoryInterface
     */
    protected $validationFactory;

    /**
     * @Inject()
     * @var ValidatorFactory
     */
    protected $ValidatorFactory;

    /**
     *
     * @param array $data
     * @param array $rules
     * @param array $messages
     * @param array $attributes
     * @param string $return_type
     * @return string
     * @throws ValidateException
     */
    protected function check(array $data = [], array $rules = [], array $messages = [], array $attributes = [], $return_type = 'http_server')
    {
        $validator = $this->ValidatorFactory->make($data, $rules, $messages, $attributes);
        if ($validator->fails()){
            $error     = $validator->errors()->first();
            if ($error&&$return_type=='api_server') {
                throw new ValidateException($error);
            }elseif ($error&&$return_type=='http_server'){
                return $error;
            }
        }
    }
}